﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>TestGet</title>
</head>
<body>

    <div>
        <button onclick="testGet()">test get请求</button>
    </div>
    <br />
    <div>
        <button onclick="testPost()">test post请求</button>
    </div>
    <br />
    <div>
        <button onclick="testMvcPostException()">测试mvc Post异常处理</button>
    </div>
    <br />
    <div>
        <button onclick="testMvcActionException()">测试mvc Action异常处理</button>
    </div>
    <br />
    <div>
        <button onclick="testApiGet()">test api get</button>
    </div>
    <br />
    <div>
        <button onclick="testApiPost()">test api post</button>
    </div>
    <br />
    <div>
        <button onclick="testApiPostException()">测试api框架异常处理</button>
    </div>
    <br />
    <div>
        <button onclick="testAsyncPost()">异步action请求</button>
    </div>
    <br />
    <div>
        <button onclick="testAsyncPostException()">异步action异常的try..catch捕获</button>
    </div>
    <br />
    <div>
        <button onclick="testCurrentUserAsync()">带token的异步请求-[FromBody]-取ajax-data正常</button>
    </div>
    <br />
    <div>
        <button onclick="testCurrentUserAsync2()">带token的异步请求-HttpContext.Current.Request["id"]-取ajax-data报错</button>
    </div>
    <br />
    <div>
        <button onclick="javascript:window.location.href = '/home/ShowLog'">查看日志</button>
    </div>
    <br />
    @Scripts.Render("~/script/common")
    <script type="text/javascript">
        function testGet() {
            $.ajax({
                url: '/api/TestDeadLockOnAwait'
                , type: 'get'
                , beforeSend: function (jqXHR, settings) {
                    //ajaxCount += 1;
                    //isLoading = true;
                }
                , complete: function (jqXHR, textStatus) {
                    //ajaxCount -= 1;
                    //isLoading = false;
                }
                , success: function (res) {
                    console.log(res.user_name);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testPost() {
            $.ajax({
                url: '/home/TestPost'
                , type: 'post'
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testMvcPostException() {
            $.ajax({
                url: '/home/testMvcPostException'
                , type: 'post'
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testMvcActionException() {
            window.location.href = '/home/testMvcActionException';
        }

        function testApiGet() {
            $.ajax({
                url: '/api/TestStringName/id01?name=goodname'
                , type: 'get'
                , data: { name: 'badname' }//get请求，会自动把data生成name=badname。此时就变成?name=goodname&name=badname，服务端会取前一个goodname
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testApiPost() {
            $.ajax({
                url: '/api/TestFromBodyName/id01'
                , type: 'post'
                //, dataType: 'json'
                , contentType: "application/json"//[FromBody]必须加上此项
                //, data: "'name02'"//或者JSON.stringify('name02')，对应服务端的[FromBody]string标记的参数
                , data: JSON.stringify({ UserId: 'id01', UserName: 'name01' })//如果服务端是一个对象
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testApiPostException() {
            $.ajax({
                url: '/api/TestEx'
                , type: 'post'
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        var asyncId = 0;
        function testAsyncPost() {
            asyncId++;
            $.ajax({
                url: '/api/TestAsyncPost/' + asyncId
                , type: 'post'
                , data: {}
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testAsyncPostException() {
            asyncId++;
            $.ajax({
                url: '/api/TestAsyncPostException/' + asyncId
                , type: 'post'
                , data: {}
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testCurrentUserAsync() {
            $.ajax({
                url: '/api/TestCurrentUserAsync/'
                , type: 'post'
                , dataType: 'json'
                , contentType: "application/json"
                , headers: {
                    token: 'Dbw%2ftuB8QIcYLwpcGNf9hAsMCL%2fuLbZJeIDQh%2bhQzrPnUN%2fCVMgU9U2N69DSOcvN'
                }
                , data: JSON.stringify({ id: '6-0' })
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
        function testCurrentUserAsync2() {
            $.ajax({
                url: '/api/TestCurrentUserAsync2/'
                , type: 'post'
                //, dataType: 'json'
                //, contentType: "application/json"
                , headers: {
                    token: 'Dbw%2ftuB8QIcYLwpcGNf9hAsMCL%2fuLbZJeIDQh%2bhQzrPnUN%2fCVMgU9U2N69DSOcvN'
                }
                , data: { id: '6-0' }
                , success: function (res) {
                    console.log(res);
                }, error: function (err, msg) {
                    alert(msg);
                }
            });
        }
    </script>
</body>
</html>
